package com.coder520.handlertest;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();
    private static final int MSG_UPDATE_COUNT = 1;
    private static final int MSG_UPDATE_PRINT = 2;
    private TextView mTextCount;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextCount = (TextView) findViewById(R.id.text_count);
        Log.d(TAG, "coder520 main thread = " + Thread.currentThread().getName());
        mThread.start();
        mHandlerThread.start();
        mNewHandler = new Handler(mHandlerThread.getLooper()) {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case MSG_UPDATE_PRINT:
                        Log.d(TAG, "coder 520 print thread = " + Thread.currentThread().getName());
                        break;
                    default:
                        break;
                }
            }
        };
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            //msg.what 表示message的action
            //msg.obj 表示message中带的数据
            //msg.arg1 表示message的参数 只能是int类型的
            //msg.arg2 message的参数, 也只能是int类型
            switch (msg.what) {
                case MSG_UPDATE_COUNT:
                    Log.d(TAG, "coder520 set text thread = " + Thread.currentThread().getName());
                    int count = msg.arg1;
                    mTextCount.setText(count + "");
                    break;
                default:
                    break;
            }
        }
    };

    private HandlerThread mHandlerThread = new HandlerThread("handler_thread");

    private Handler mNewHandler;

    private Thread mThread = new Thread("thread_count") {
        private int mCount = 1;
        @Override
        public void run() {
            while(true) {
//                Message message = new Message();
//                message.what = MSG_UPDATE_COUNT;
//                message.arg1 = mCount++;
//                mHandler.sendMessage(message);
                Log.d(TAG, "coder520 count thread = " + Thread.currentThread().getName());
                Message message = mHandler.obtainMessage(MSG_UPDATE_COUNT, mCount++, 0);
                message.sendToTarget();
                Message message2 = mNewHandler.obtainMessage(MSG_UPDATE_PRINT);
                message2.sendToTarget();

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };
}
